<%@ page language="java" pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sec"
	uri="http://www.springframework.org/security/tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%
    String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";
%>
<%@ include file="/inc.jsp" %>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>闪耀先生-我的订单</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />    
    <!-- style -->
    <link href="${pageContext.request.contextPath }/vip/css/usercenter.css" rel="stylesheet">
  </head>
  <body>
  <s:iterator value="pageOrder.elements" status="stat">
   <div class="block order">
      <div class="state">
        <s:date name="createTime" format="yyyy-MM-dd HH:ss" /><span class="org">
      </div><!--E .state-->
      <div class="orderinfo">
        <div class="picbox"><img src="<s:property value="brand_img" />"></div><!--E .picbox-->
        <div class="proinfo">
          <h3><s:property value="brand" /></h3>
          <div class="pri">价格：<s:property value="price" />元</div>
        </div><!--E .proinfo-->
        <s:if test="payment==0">
	        <div class="paybtn"> 
	          <button type="button" class="btn-org" onclick="javascript:_subPay('<s:property value="id" />');">支付</button>
	           </div><!-- E .paybtn -->
        </s:if>
        <s:else>
        	
        <div class="paybtn">
	          <a href="${pageContext.request.contextPath }/public/vipAction!myOrderDetail.action?orderId=<s:property value="id" />"><img src="${pageContext.request.contextPath }/vip/images/arrow.png"></a>
	        </div><!-- E .paybtn -->
        </s:else>
      </div><!--E .orderinfo-->
    </div><!--E .order-->
  </s:iterator>
	 <s:if test=" pageOrder.elements.isEmpty() ">
	       <div class="block order">
		      <h3>暂无订单，快去选购喜欢的商品。</p><!--适用于拥有金纽扣的用户，订单为空时-->
		    </div><!--E .order-->
	 </s:if>							
   
  </body>
	<script src="<%=basePath%>js/jquery-1.10.1.min.js"></script>
 <script>
 function _subPay(orderId){
	 $.ajax({
			type:'post',
			url:'<%=basePath%>public/vipAction!payOrder.action',
			dataType : 'json', 
			data : {"openId":'${openId}',"orderId":orderId},
			timeout:60000,
			success: function (json_str){
				  var temp = eval(json_str);  
				  if(temp.code == "success"){
					  WeixinJSBridge.invoke('getBrandWCPayRequest',
						  	   {
						  	   "appId" : temp.appId,     //公众号名称，由商户传入     
					           "timeStamp":temp.timeStamp,         //时间戳，自1970年以来的秒数     
					           "nonceStr" : temp.nonceStr, //随机串     
					           "package" : temp.package,     
					           "signType" : "MD5",         //微信签名方式:     
					           "paySign" : temp.paySign//微信签名 
						  	   },
						  	  function(res){
		                          if(res.err_msg == "get_brand_wcpay_request:ok" ) {
		                          	//交易成功
		                          	if(temp.orderType == "vip"){
		                          		_updateVip(temp.orderId);
		                          	}else if(temp.orderType == "tmall-s"){
		                          		_updateSorder(temp.orderId);
		                          	}else if(temp.orderType == "tmall-ns"){
		                          		_updateOrder(temp.orderId);
		                          	}else{
		                          		alert("交易失败!找不到订单:"+temp.orderId);
		                          	}
		                          }else{
		                          	alert("交易失败!"+res.err_msg);
		                       		 location.href = "${pageContext.request.contextPath}/public/vipAction!myOrderWx.action";
		                          }
		                          // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
		                          //因此微信团队建议，当收到ok返回时，向商户后台询问是否收到交易成功的通知，若收到通知，前端展示交易成功的界面；若此时未收到通知，商户后台主动调用查询订单接口，查询订单的当前状态，并反馈给前端展示相应的界面。
	                          }); 
				  }else{
					  alert(temp.message);
				  }
			}
		});
 }
 function _updateVip(orderId){
	 $.ajax({
			type:'post',
			url:'<%=basePath%>public/vipAction!updateVip.action',
			dataType : 'json', 
			data : {"openId":'${openId}',"fdType":"50","orderId":orderId},
			timeout:60000,
			success: function (json_str){
				//alert(json_str);
				
				
			}
		});
	 location.href = "${pageContext.request.contextPath}/public/vipAction!myOrderWx.action";
 }
function _updateSorder(orderId){
	 $.ajax({
			type:'post',
			url:'<%=basePath%>public/orderSingleAction!updateOrder.action',
						dataType : 'json',
						data : {
							"fdOrderId" : orderId,"openid":'${openId}'
						},
						timeout : 60000,
						success : function(json_str) {
							 var temp = eval(json_str);  
							 alert(temp.message);
							 location.href = "${pageContext.request.contextPath}/public/vipAction!myOrderWx.action";
						}
					});
		}

function _updateOrder(orderId){
	 $.ajax({
			type:'post',
			url:'<%=basePath%>public/orderAction!updateOrder.action',
				dataType : 'json',
				data : {
					"fdOrderId" : orderId,"openid":'${openId}'
				},
				timeout : 60000,
				success : function(json_str) {
					 var temp = eval(json_str);  
					 alert(temp.message);
					 location.href = "${pageContext.request.contextPath}/public/vipAction!myOrderWx.action";
				}
			});
		}
 </script>
</html>